package com.dkts.oil.util.gis;

import org.apache.commons.lang3.StringUtils;
import org.geotools.geojson.geom.GeometryJSON;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.operation.buffer.BufferOp;

import java.util.List;

/**
 * @author: heguanglong
 * @date: 2020-12-26
 **/
public class GpsSpiltUtils {
    public static GeometryFactory geometryFactory = new GeometryFactory();

    public static WKTReader reader = new WKTReader();

    public static GeometryJSON geometryJSON = new GeometryJSON();
    public static double baseNum = 100000;
    public static double rate = 0.25;

    public static double radius(double radius) {
        return radius / (2 * Math.PI * 6371004) * 360;
    }

    public static synchronized MultiPolygon getMultiWkt(String wkr) throws ParseException {
        if(StringUtils.isBlank(wkr)){
            return null;
        }
        if(wkr.startsWith("MULTIPOLYGON")){
            MultiPolygon multiPolygon = (MultiPolygon) reader.read(wkr);
            return multiPolygon;
//            Polygon[] polygons = new Polygon[multiPolygon.getNumGeometries()];
//            for (int i = 0; i < multiPolygon.getNumGeometries(); i++) {
//                Polygon polygon = (Polygon) multiPolygon.getGeometryN(i);
//                polygons[i] = getCoordinate(polygon);
//            }
//            return geometryFactory.createMultiPolygon(polygons);
        }else if(wkr.startsWith("POLYGON")) {
            Polygon[] polygons = new Polygon[1];
//            polygons[0] =getCoordinate((Polygon) reader.read(wkr));
            polygons[0] = (Polygon) reader.read(wkr);
            return geometryFactory.createMultiPolygon(polygons);
        }
        return null;
    }

    public static Polygon getCoordinate(Polygon polygon) {
        Coordinate[] coordinates = polygon.getCoordinates();
        Coordinate[] coordinatesNew = new Coordinate[coordinates.length];
        for (int j = 0; j < coordinates.length; j++) {
            Coordinate coordinate = coordinates[j];
            GpsPoint point=GpsFormat.wgs84ToGcj02(coordinate.x, coordinate.y);
            coordinatesNew[j] = new Coordinate(point.getLng(), point.getLat());
        }
        return geometryFactory.createPolygon(coordinatesNew);
    }
    /**
     * 经纬度转换 jts识别的点
     *
     * @param str
     * @return
     */
    public static Coordinate[] getCoordinate(String str) {
        String[] arr=null;
        if(str.contains(";")) {
            arr = str.split(";");
        }else if (str.contains(",")){
            arr = str.split(",");
        }
        if (arr == null){
            return null;
        }
        Coordinate[] coordinates4 = new Coordinate[arr.length];
        for (int i = 0; i < arr.length; i++) {
            String[] a = arr[i].split(" ");
            if(a.length == 1){
                a= a[0].split(",");
            }
            Coordinate c = new Coordinate(Double.valueOf(a[0]), Double.valueOf(a[1]));
            coordinates4[i] = c;
        }
        return coordinates4;
    }

    public static Polygon getPolygon(String str) {
        Coordinate[] coordinates = getCoordinate(str);
        return geometryFactory.createPolygon(coordinates);
    }

    public static MultiPolygon getMultiPolygon(List<String> gps) {
        if (gps == null || gps.size() <= 0) {
            System.out.println("gps为空");
            return geometryFactory.createMultiPolygon(null);
        }
        int row = gps.size();
        Polygon[] polygons = new Polygon[row];
        for (int i = 0; i < row; i++) {
            polygons[i] = getPolygon(gps.get(i));
        }
        return geometryFactory.createMultiPolygon(polygons);
    }

    /**
     * 获取缓冲区域
     *
     * @param gmt  实体图
     * @param type
     * @param rd   缓冲去距离
     * @return
     */
    public static Geometry getBuff(Geometry gmt, int type, double rd) {
        BufferOp bufOp = new BufferOp(gmt);
        bufOp.setEndCapStyle(type);
        bufOp.setQuadrantSegments(18);
        return bufOp.getResultGeometry(radius(rd));
    }

    /**
     * 获取缓冲区域转换成数组
     */
    public static String gpsToString(Coordinate[] gps) {
        if (gps != null) {
            StringBuilder stringBuilderBuffer = new StringBuilder();
            for (Coordinate currentCoordinates : gps) {
                double lat = currentCoordinates.x;
                double lon = currentCoordinates.y;
                stringBuilderBuffer.append(lat).append(",").append(lon).append(";");
            }
            return stringBuilderBuffer.deleteCharAt(stringBuilderBuffer.length() - 1).toString();
        }
        return null;
    }
}
